package hu.mapro.gwtui.client.impl;

import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.Widget;

import hu.mapro.gwtui.client.Action;
import hu.mapro.gwtui.client.Page;
import hu.mapro.gwtui.client.action.Publisher;

abstract public class TabPage implements Page {

	HasText label;
	
	protected TabPage(HasText label) {
		super();
		this.label = label;
	}

	boolean added = false;
	
	protected Widget widget;
	
	Publisher attachListeners = new Publisher();
	
	@Override
	public void show() {
		if (added) {
			activateTab();
		} else {
			addTab();
			activateTab();
			added = true;
			attachListeners.fire();
		}
	}

	@Override
	public void hide() {
		if (added) {
			removeTab();
			added = false;
		}
	}

	abstract protected void removeTab();

	abstract protected void addTab();
	
	abstract protected void activateTab();
	
	@Override
	public void setWidget(Widget widget) {
		this.widget = widget;
	}

	@Override
	public void addAttachHandler(Action action) {
		attachListeners.add(action);
	}

	@Override
	public String getText() {
		return label.getText();
	}

	@Override
	public void setText(String text) {
		label.setText(text);
	}

}
